package com.trepadora.flex
{
	import flash.events.NetStatusEvent;
	import flash.events.TimerEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	import flash.net.NetConnection;
	import flash.utils.Timer;
	
	import mx.core.Application;

	public class AVManager
	{
		public static var userId:int;
		private static var rtmpAddress:String="rtmp://sigma.ug.edu.pl/oflaDemo";
		private static var nc:NetConnection;
		private static var first:Boolean=true;

		public static function init():void
		{
			nc=new NetConnection();
			nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);

			connect();
		}


		public static function getNc():NetConnection
		{
			return nc;
		}

		public static function connect():void
		{
			nc.connect(rtmpAddress);
		}

		public static function netStatusHandler(event:NetStatusEvent):void
		{
			trace("KOD" + event.info.code)
			switch (event.info.code)
			{
				case "NetConnection.Connect.Success":
					trace("FMS CONNECTION SUCCESS")
					break;
				case "NetConnection.Connect.Failed":
					trace("FMS CONNECTION FAILED")
					var timer:Timer=new Timer(1000, 1);
					timer.addEventListener(TimerEvent.TIMER, startConnect);
					timer.start();
					break;
				case "NetConnection.Connect.Closed":
					var timer:Timer=new Timer(1000, 1);
					timer.addEventListener(TimerEvent.TIMER, startConnect);
					timer.start();
					break;
			}

		}

		private static function startConnect(event:TimerEvent):void
		{
			connect();
		}

		public static function getCamera(cameraName:String):Camera
		{
			var cams:Array=Camera.names;

			for (var i:int=0; i < cams.length; i++)
			{
				if (cameraName == cams[i])
				{
					return Camera.getCamera("" + i);
				}
			}
			return null;
		}

		public static function getMicrophone(micName:String):Microphone
		{
			var mics:Array=Microphone.names;

			for (var i:int=0; i < mics.length; i++)
			{
				if (micName == mics[i])
				{
					return Microphone.getMicrophone(i);
				}
			}
			return null;
		}
	}
}